#include "thread_pool.h"
#include <dirent.h>
#include <mysql/mysql.h>
#include <unistd.h>

extern MYSQL* conn;

void lsCommand(task_t * task){
    int userid = 0;
    int pwdid = 0;

    //得到userid
    int ret = getUserid(task->peerfd, &userid);
    if(ret){
        fprintf(stderr, "Get userid error\n");
        sendMsg(task->peerfd, MSG_TYPE_FAILURE, 
                "An error occurred while retrieving the user name.");
        return ;
    }
    printf("userid %d\n", userid);

    //查询得到pwdid
    char query[256] = {0};
    sprintf(query,
            "SELECT pwdid FROM t_user WHERE userid=%d", userid);
    ret = mysql_query(conn, query);
    MYSQL_RES * res = mysql_store_result(conn);
    MYSQL_ROW row = mysql_fetch_row(res);
    pwdid = atoi(row[0]);
    mysql_free_result(res);
    printf("pwdid: %d\n", pwdid);

    //查询当前目录下所有的文件
    memset(query, 0, sizeof(query));
    sprintf(query,
            "SELECT filename FROM t_file WHERE owner_id=%d AND parent_id=%d",
            userid, pwdid);
    mysql_query(conn, query);
    res = mysql_store_result(conn);
    
    char buf[1000] = {0};
    strcat(buf,"\t");
    while((row = mysql_fetch_row(res)) != NULL){
        strcat(buf, row[0]);
        strcat(buf, "\t");
    }

    mysql_free_result(res);
    
    sendMsg(task->peerfd, MSG_TYPE_SUCCESS, buf);

}




int ret_filename_str(char* curPath, char* result){
    DIR* dir = opendir(curPath);
    if(!dir){
        // 目录流打开失败时
        // 会返回NULL, 并设置errno
        strcpy(result,"opendir failure");
        return -1;
    }
    //读取当前路径的文件或目录名
    int count = 0;
    struct dirent *entry = NULL;

    while((entry = readdir(dir)) != NULL){
        // 将文件名拼接在一起
        if(entry->d_name[0] == '.')
            continue;
        strcat(result, entry->d_name);
        count++;
        // if(count % 5 == 0)
        //     strcat(result, "\n");
        // else
        strcat(result, "\t");
    }
    //关闭目录
    closedir(dir);
    return 0;
}

void lsCommand1(task_t * task)
{
    char result[10240] = {'\0'};
    char curPath[128] = {'\0'};
    // int ret = 0;
    getcwd(curPath, sizeof(curPath));
    // 先判断所属 ls 类型
    if(strlen(task->data) == 0){
        // ls 类型
        ret_filename_str(curPath, result);
        fprintf(stderr, "strlen(task->data): %lu.\n", strlen(task->data));
    }
    else{
        if((task->data)[0] != '-'){
            // ls path类型
            // 需要先判断传过来的路径是否合法
            DIR* dir = opendir(task->data);
            if(!dir){
                // 传过来的路径非法，直接返回client路径非法信息
                sscanf(result, "cannot find path: %s.\n", task->data);
            }else{
                // 路径合法
                // 先关闭目录流
                closedir(dir);
                // 将path传过去
                ret_filename_str(task->data, result);
            }
        }
        else{
            // 说明是ls -alh类型，需要继续分类

        }
    }    
    train_t t;
    memset(&t, 0, sizeof(t));
    t.len = strlen(result);
    strcpy(t.buff, result);
    sendn(task->peerfd, &t, sizeof(t.len) + strlen(t.buff));
}

